#符达康
from flask import Flask, jsonify, request, render_template
import json

db_file_name = 'people-file'

def storeDbase(db,db_file_name=db_file_name):
    "将数据库格式化为json格式文件"
    dbfile = open(db_file_name,'w')
    json.dump(db,dbfile)
    dbfile.close

def loadDbase(db_file_name=db_file_name):
    "将json格式文件加载为python数据"
    dbfile = open(db_file_name,'r')
    json_data = json.load(dbfile)
    return json_data

students = loadDbase()
courses = []

courses = [
    {'id':1,'name':'python程序设计','teacher':'张良峰','age':64,'week':'星期三','address':'4-411'}
    ]


app = Flask(__name__)

@app.route('/') # 获取学生信息页面
def index():
    return render_template('index.html')

@app.route('/cos') # 获取课程信息页面
def cos():
    return render_template('course.html')

@app.route('/nav')
def nav():
    return render_template('nav.html')

# 列表展示学生信息
@app.route('/students', methods=['GET'])
def get_students():
    return jsonify(students)

# 列表展示课程信息
@app.route('/courses', methods=['GET'])
def get_courses():
    return jsonify(courses)

# 添加学生信息
@app.route('/students', methods=['POST'])
def add_student():
    new_student = {
        'id': int(request.json['id']),
        'name': request.json['name'],
        'gender': request.json['gender'],
        'age': request.json['age'],
        'id_card': request.json['id_card'],
        'phone': request.json['phone'],
        'address': request.json['address']
    }
    students.append(new_student)
    storeDbase(students)
    return jsonify(new_student)

# 添加课程信息
@app.route('/courses', methods=['POST'])
def add_course():
    new_course = {
        'id': int(request.json['id']),
        'name': request.json['name'],
        'gender': request.json['gender'],
        'age': request.json['age'],
        'id_card': request.json['id_card'],
        'phone': request.json['phone'],
        'address': request.json['address']
    }
    courses.append(new_course)
    return jsonify(new_course)

# 获取单个学生信息
@app.route('/students/<int:id>', methods=['GET'])
def get_student(id):
    for student in students:
        if student['id'] == id:
            return jsonify(student)
    return jsonify({'error': 'Student not found'})

# 修改学生信息
@app.route('/students/<int:id>', methods=['PUT'])
def update_student(id):
    for student in students:
        if student['id'] == id:
            student['name'] = request.json['name']
            student['gender'] = request.json['gender']
            student['age'] = request.json['age']
            student['id_card'] = request.json['id_card']
            student['phone'] = request.json['phone']
            student['address'] = request.json['address']
            storeDbase(students)
            return jsonify(student)
    return jsonify({'error': 'Student not found'})

# 删除学生信息
@app.route('/students/<int:id>', methods=['DELETE'])
def delete_student(id):
    for student in students:
        if student['id'] == id:
            students.remove(student)
            storeDbase(students)
            return jsonify({'result': True})
    return jsonify({'error': 'Student not found'})

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=80)
